package com.hua.nailcampus.acl.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.hua.nailcampus.acl.model.Approval;
import com.hua.nailcampus.acl.model.Role;
import com.hua.nailcampus.acl.model.vo.ApprovalVo;
import com.hua.nailcampus.acl.service.ApprovalService;
import com.hua.nailcampus.acl.service.RoleService;
import com.hua.nailcampus.utils.JwtHelper;
import com.hua.nailcampus.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.TimeZone;

/**
 * @Author:chenjianhua
 * @Date:2023/12/1 22:32
 * @Version 1.8
 */
@RestController
@Slf4j
@RequestMapping("/nailCampus/approval")
public class ApprovalController {

    @Autowired
    private ApprovalService approvalService;

    @Autowired
    private RoleService roleService;

    /**
     * 提交申请表之后,直接保存到数据库
     * @param approval
     * @return
     */
    @PostMapping("/saveApproval")
    public Result saveApproval(@RequestBody Approval approval) {
        log.info("参数approval:{}", approval);
        approval.setStep(1);
        approvalService.saveApproval(approval);
        return Result.ok(null);
    }

    /**
     * 更新审批进度
     *
     * @param request
     * @param approval
     * @return
     */


    @PostMapping("updateApproval")
    public Result update(HttpServletRequest request,
                         @RequestBody Approval approval) {
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        log.info("参数approval:{}", approval);
//        判断当前操作为教师操作还是辅导员操作
        if (approval.getStep().equals(2)) {
//           若为教师操作，则设置教师审批者为该用户ID
            DateTime date = DateUtil.date().setTimeZone(TimeZone.getTimeZone("GMT+8"));
            approval.setSecondStepDate(date);
            approval.setSecondStepUserId(userId);
        }
        if (approval.getStep().equals(3)) {
//            若为辅导员操作，则设置辅导员审批者为该用户ID
            DateTime date = DateUtil.date().setTimeZone(TimeZone.getTimeZone("GMT+8"));
            approval.setThirdStepDate(date);
            approval.setThirdStepUserId(userId);
        }
//        更新数据库
        approvalService.updateApproval(approval);
        return Result.ok(null);
    }

    /**
     * 根据用户角色获取审批单
     * 普通学生只能查看自己的提交过的申请单
     * 教师角色获取学生提交且是自己处理的申请单
     * 辅导员角色获取学生提交且是自己处理的申请单
     * @return
     */
    @GetMapping("/getApproval")
    public Result getApprovalById(HttpServletRequest request) {
//        获取请求头中携带的token
        String token = request.getHeader("token");
//        获取token中的用户ID
        Integer userId = JwtHelper.getUserId(token);
//        获取当前用户ID的角色
        Role role =  roleService.getRoleByUserId(userId);
//        根据不同的角色来返回不一样的数据
        List<Approval> approvalList = approvalService.getApproval(role);
        return Result.ok(approvalList);
    }

    /**
     * 教师与辅导员获取未审批的表单
     * @param request
     * @return
     */
    @GetMapping("/getUnProcessApproval")
    public Result getUnProcessApproval(HttpServletRequest request){

        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
//        获取用户角色
        Role role = roleService.getRoleByUserId(userId);
//        获取学校ID
        List<ApprovalVo> approvalList =approvalService.getUnProcessApproval(userId,role);
        return Result.ok(approvalList);
    }

    /**
     * 若用户角色为学生，则返回自己的申请单，
     * 若用户角色为教师或辅导员，则返回用户审批过或者正在审批的申请单
     * @param request
     * @return
     */
    @GetMapping("getUserApproval")
    public Result getUserApproval(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        Role role = roleService.getRoleByUserId(userId);
        List<ApprovalVo> approvalVoList = approvalService.getUserApproval(userId,role.getCode());
        return Result.ok(approvalVoList);
    }

    @GetMapping("getApprovalById/{id}")
    public Result getApprovalById( @PathVariable Integer id){
        ApprovalVo approval = approvalService.getApprovalById(id);
        return Result.ok(approval);
    }
}

